package com.eee.machine.service.impl;

import com.eee.machine.beans.TUser;
import com.eee.machine.common.OperateResponse;
import com.eee.machine.configuration.StoreConfig;
import com.eee.machine.dao.mapper.TUserMapper;
import com.eee.machine.service.IUserService;
import com.eee.machine.utils.cache.SimpleCache;
import com.eee.machine.utils.youzan.TokenUtil;
import com.youzan.open.sdk.api.APIFactory;
import com.youzan.open.sdk.client.auth.Token;
import com.youzan.open.sdk.client.core.DefaultYZClient;
import com.youzan.open.sdk.client.core.YZClient;
import com.youzan.open.sdk.client.oauth.model.OAuthToken;
import com.youzan.open.sdk.gen.v3_0_0.api.YouzanUserBasicGet;
import com.youzan.open.sdk.gen.v3_0_0.model.YouzanUserBasicGetParams;
import com.youzan.open.sdk.gen.v3_0_0.model.YouzanUserBasicGetResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

@Service
public class IUserServiceImp implements IUserService {

    @Autowired
    private TokenUtil tokenUtil;
    @Autowired
    private TUserMapper userMapper;
    @Autowired
    private HttpSession session;
    @Autowired
    private StoreConfig storeConfig;


    @Override
    public OperateResponse checkAdminLogin(String phone, String password) {
        TUser user = userMapper.getUserByPhoneAndPassword(phone, password);
        if(user != null){
            SimpleCache<TUser> instance = SimpleCache.getInstance();
            Calendar ca = Calendar.getInstance();
            ca.add(Calendar.DATE, 1);// num为增加的天数，可以改变的
            instance.addCache("userInfo",user,ca.getTime(),false);
            session.setAttribute("loginUser",user);
            Map<String, Object> map = new HashMap<>();
            map.put("user", user);
            map.put("redirectUrl", APIFactory.SERVICE_HOST + "/oauth/authorize?client_id=" + storeConfig.getClientId()
                    + "&response_type=code&state="+storeConfig.getState()+"&redirect_uri="+storeConfig.getRedirectUrl());
            return OperateResponse.obtainSuccessResponse(map);
        }
        return null;
    }

    @Override
    public OperateResponse getAllDistributionMember() {
//        OAuthToken token = TokenUtil.getToken();
//        YZClient client = new DefaultYZClient(new Token(token.getAccessToken())); //new Sign(appKey, appSecret)
        String accessToken = tokenUtil.getAccessToken();
        System.out.println("授权token====="+accessToken);
        YZClient client = new DefaultYZClient(new Token(accessToken)); //new Sign(appKey, appSecret)
        YouzanUserBasicGetParams youzanUserBasicGetParams = new YouzanUserBasicGetParams();
        YouzanUserBasicGet youzanUserBasicGet = new YouzanUserBasicGet();
        youzanUserBasicGet.setAPIParams(youzanUserBasicGetParams);
        YouzanUserBasicGetResult result = client.invoke(youzanUserBasicGet);
        return OperateResponse.obtainSuccessResponse(result);
    }
}
